/**
 * 
 */
package com.demo2do.boyle.backend.web.controller.aduser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.aduser.AduserLevelService;
import com.demo2do.boyle.common.entity.aduser.AduserLevel;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;

/**
 * @author Leo
 *
 */

@Controller
@RequestMapping("/aduser-levels")
@MenuSetting("menu-aduser")
public class AduserLevelController {

	@Autowired
	private AduserLevelService aduserLevelService;
	
	private static final Logger logger = LoggerFactory.getLogger(AduserLevelController.class);
	
	/**
	 * List all aduser levels
	 * 
	 * @param page
	 * @return  
	 */
	@RequestMapping("")
	@MenuSetting("submenu-aduser-level-list")
	public String list() {
		return "aduser/level/aduser-level-list";
	}

	/**
	 * display the edit panel
     * 
     * @param id
     * @return the aduserLevel info edit view
     */
    @RequestMapping("/{id}/edit")
    @MenuSetting("submenu-aduser-level-list")
    public ModelAndView edit(@PathVariable("id") Integer id) {
    	
        ModelAndView modelAndView = new ModelAndView("aduser/level/aduser-level-edit");
        
        AduserLevel aduserLevel = aduserLevelService.load(AduserLevel.class, id);
        modelAndView.addObject("id", id);
        modelAndView.addObject("aduserLevel", aduserLevel);
        
        return modelAndView;
    }
    
    /**
     * do edit
     * 
     * @param id
     * @param aduserLevel
     * @return the result
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
	public @ResponseBody String onEdit(@Preloading(value = "id", type = Integer.class) AduserLevel aduserLevel) {
    	
		Result result = new Result();		
		
		try {

			aduserLevelService.update(aduserLevel);
            result.success().message("更新成功");

        } catch(Exception e) {
            result.fail().message("服务器异常");
            logger.info("onEditWallet()", e);
        }
		
		return JsonUtils.toJsonString(result);
	}
    
    /**
     * display the create panel
     * 
     * @return
     */
    @RequestMapping("/create")
    public String create() {
        return "aduser/level/aduser-level-create";
    }
    
    /**
     * create a new aduserLevel
     * 
     * @param aduserLevel
     * @return the result
     */
    @RequestMapping( value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(AduserLevel aduserLevel) {
    	
    	Result result = new Result();
    		
    	aduserLevelService.save(aduserLevel);
    		
    	result.success().message("创建用户级别成功！");
    	
    	return JsonUtils.toJsonString(result);    	
    
    }
	
    /**
	 * display the primary panel
	 * 
	 * @return the update-primary view
	 */
    @RequestMapping("/{id}/primary")
    public ModelAndView primary(@PathVariable("id") Integer id) {
    	
    	ModelAndView modelAndView = new ModelAndView("aduser/level/aduser-level-primary");
    	modelAndView.addObject("id", id);
    	
    	return modelAndView;
    }    
    
    /**
     * do primary 
     * 
     * @param id
     * @return the result
     */
    @RequestMapping( value = "/{id}/primary", method = RequestMethod.PATCH)
    public @ResponseBody String onPrimary(@PathVariable("id") Integer id) {
    	
    	Result result = new Result();
    			
    	AduserLevel aduserLevel = aduserLevelService.load(AduserLevel.class, id);
    	
    	aduserLevelService.primary(aduserLevel);
    	
    	result.success().message("设置默认级别成功！");
    	
    	return JsonUtils.toJsonString(result);
    	
    }
    
}
